package com.cskaoyan.wordmemorize.excel;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cskaoyan.wordmemorize.common.constant.CommonConstant;
import com.cskaoyan.wordmemorize.converter.TopicConverter;
import com.cskaoyan.wordmemorize.dao.entity.ReviewTopicDO;
import com.cskaoyan.wordmemorize.dao.entity.TopicOptionDO;
import com.cskaoyan.wordmemorize.dao.entity.VocDO;
import com.cskaoyan.wordmemorize.dao.mapper.ReviewTopicMapper;
import com.cskaoyan.wordmemorize.dao.mapper.TopicOptionsMapper;
import com.cskaoyan.wordmemorize.dao.mapper.VocMapper;
import com.cskaoyan.wordmemorize.dto.admin.SingleTopicExcelDTO;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.Arrays;
import java.util.List;

@Component
public class SingleChoiceExcelSaver extends AbstractExcelSaver<SingleTopicExcelDTO, SingleTopicExcelDTO>{


    @Resource
    TransactionTemplate transactionTemplate;

    @Resource
    ReviewTopicMapper reviewTopicMapper;

    @Resource
    TopicConverter topicConverter;

    @Resource
    VocMapper vocMapper;

    @Resource
    TopicOptionsMapper topicOptionsMapper;

    @PostConstruct
    public void init() {
        super.init(CommonConstant.BATCH, transactionTemplate);
    }


    @Override
    public void doSave(List<SingleTopicExcelDTO> targetList) {

        for (int i = 0; i < targetList.size(); i++) {


            SingleTopicExcelDTO singleTopicExcelDTO = targetList.get(i);
            List<String> options = Arrays.asList(singleTopicExcelDTO.getOption1(), singleTopicExcelDTO.getOption2(), singleTopicExcelDTO.getOption3(),
                    singleTopicExcelDTO.getOption4());
            // 保存题目
            LambdaQueryWrapper<VocDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VocDO::getContent, options.get(singleTopicExcelDTO.getAnswerIndex()))
                    .select(VocDO::getId);
            VocDO vocDO = vocMapper.selectOne(queryWrapper);

            ReviewTopicDO reviewTopicDO = topicConverter
                    .singleTopicExcelDTO2DO(singleTopicExcelDTO, vocDO.getId());
            reviewTopicDO.setVocabulary(options.get(singleTopicExcelDTO.getAnswerIndex()));
            reviewTopicMapper.insert(reviewTopicDO);

            // 保存选项

            saveSingleChoiceOptions(options, reviewTopicDO.getId(), singleTopicExcelDTO.getAnswerIndex());
        }

    }

    private void saveSingleChoiceOptions (List<String> options, Long tid, Integer answerIndex) {
        for (int i = 1; i <= options.size(); i++ ) {
            String option = options.get(i - 1);
            TopicOptionDO topicOptionDO = new TopicOptionDO();
            topicOptionDO.setTid(tid);
            topicOptionDO.setItem(option);
            topicOptionDO.setIsAnswer(answerIndex == i ? 1: 0);
            topicOptionDO.setOptionNumber(i);
            topicOptionsMapper.insert(topicOptionDO);
        }
    }
}
